{{- /*
GitHub file content.

@param {string} file Path to file on GitHub.
@param {string} ref Branch, tag or commit.
@param {string} lang Syntax highlighting. If not specified, the syntax is determined by the file extension.

@returns {template.HTML}

@examples

  {{< gh-file file="github/docs/README.md" lang="md" >}}
*/ -}}

{{- if (.IsNamedParams) -}}

    {{ $file := (.Get "file") }}
    {{ $ref := ((.Get "ref") | default "main") }}
    {{ $lang := (.Get "lang") }}
    {{ $api := (site.Params.api) }}
    {{ $ts := (now.Unix) }}
    {{ $headers := (dict
    "User-Agent" ($api.user_agent)
    "Authorization" (printf "Bearer %s" (($api.github.token) | base64Decode))
    ) }}

    {{ $data := (newScratch) }}
    {{ range ((split $file "/") | first 2) }}{{ ($data.Add "repo" (slice .)) }}{{ end }}
    {{ range ((split $file "/") | after 2) }}{{ ($data.Add "path" (slice .)) }}{{ end }}
    {{ $file = (getJSON ($api.github.url) "/repos/" (delimit ($data.Get "repo") "/") "/contents/" (delimit ($data.Get "path") "/") "?ref=" ($ref) "&ts=" ($ts) ($headers)) }}
    {{ $cp := ((printf "clipboard-%s-%s" ($.Name) (delimit (shuffle (seq 11 99)) "")) | md5) }}

    <div class="shortcode shortcode-{{ ($.Name) }} mb-3">
      <div class="card overflow-hidden">
        <div class="card-header">
          <div class="d-flex align-items-center">
            <div class="flex-shrink-0">
              <i class="fab fa-github fa-fw"></i>
            </div>
            <div class="flex-grow-1 mx-2 text-truncate">
              <code>{{ ($file.path) }}</code>
            </div>
            <div class="flex-shrink-0">
              <ul class="list-inline mb-0">
                  {{ with ($file.size) }}
                    <li class="list-inline-item">
                      <code>{{ (div . 1000) }} {{ (i18n "sc.github.file.kB") }}</code>
                    </li>
                  {{ end }}
                <li class="list-inline-item" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.file.copy") }}'>
                  <a class="text-body" href="#"
                     data-fn="clipboard preventDefault" data-clipboard-target="#cp-{{ ($cp) }}"
                     role="button" aria-label='{{ (i18n "sc.github.file.copy") }}'>
                    <i class="fas fa-copy fa-fw"></i>
                  </a>
                </li>
                  {{ with ($file.download_url) }}
                    <li class="list-inline-item" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.file.download") }}'>
                      <a class="text-body" href="{{ . }}"
                         target="_blank" rel="noopener nofollow"
                         role="button" aria-label='{{ (i18n "sc.github.file.download") }}'>
                        <i class="fas fa-download fa-fw"></i>
                      </a>
                    </li>
                  {{ end }}
                  {{ with ($file.html_url) }}
                    <li class="list-inline-item" data-bs-tooltip data-bs-title='{{ (i18n "sc.github.file.link") }}'>
                      <a class="text-body" href="{{ . }}"
                         target="_blank" rel="noopener nofollow"
                         role="button" aria-label='{{ (i18n "sc.github.file.link") }}'>
                        <i class="fas fa-arrow-up-right-from-square fa-fw"></i>
                      </a>
                    </li>
                  {{ end }}
              </ul>
            </div>
          </div>
        </div>
        <div class="card-body" id="cp-{{ ($cp) }}">
            {{ if (eq (($lang) | len) 0) }}
                {{ $lang = ((replace (path.Ext (delimit ($data.Get "path") "/")) "." "") | default "plaintext") }}
            {{ end }}
            {{ (highlight ((($file.content) | base64Decode) | chomp) ($lang)) }}
        </div>
      </div>
    </div>

{{- else -}}

    {{ (partial "alerts/no_params" .) }}

{{- end -}}
